<template>
  <div class="goods">
    <el-button @click="goAdd" plain type="success"  class="addBtn">添加商品</el-button>

    <!-- 表格 -->
    <el-table :data="goodList">
      <el-table-column type="index" label="#" :index="indexMethod"></el-table-column>
      <el-table-column prop="goods_name" label="商品名称"></el-table-column>
      <el-table-column prop="goods_price" label="商品价格"></el-table-column>
      <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
      <el-table-column  label="创建时间">
         <template v-slot="{row}">
          {{row.add_time | time}}
        </template>
      </el-table-column>
      <el-table-column  label="操作">
          <el-button type="primary" circle plain icon="el-icon-edit" size="small"></el-button>
        <el-button type="danger" circle plain icon="el-icon-delete" size="small"></el-button>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
    background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      pagenum: 1,
      pagesize: 10,
      goodList: [],
      total: 0

    }
  },
  methods: {
    async getGoodList () {
      const { meta, data } = await this.$axios.get('goods', {
        params: {
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      // console.log(meta, data)
      if (meta.status === 200) {
        this.goodList = data.goods
        this.total = data.total
      }
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagenum = 1
      this.pagesize = val
      this.getGoodList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.pagenum = val
      this.getGoodList()
    },
    indexMethod (index) {
      return (this.pagenum - 1) * this.pagesize + index + 1
    },
    goAdd () {
      // 不管/goods-xxxx  都让/goods亮
      this.$router.push('/goods-add')
    }
  },
  created () {
    this.getGoodList()
  }
}
</script>

<style lang="less" scoped>
  .addBtn{
    margin-bottom: 10px;
  }
//   .el-pagination {
//   /deep/ .active {
//     background-color: green !important;
//   }
// }
</style>
